using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Controls_Session : System.Web.UI.UserControl
{
    private bool _sessionActive = false;
    public bool SessionActive
    {
        get { return _sessionActive; }
        set { _sessionActive = value; }
    }
    private SessionMode _mode = SessionMode.Private;
    public SessionMode Mode
    {
        get { return _mode; }
        set { _mode = value; }
    }
    public Controls_Session()
    {
        this.PreRender += new EventHandler(Controls_Session_PreRender);
    }

    void Controls_Session_PreRender(object sender, EventArgs e)
    {
        imgUser.Src = ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/user.gif";
        imgUser.Style.Add(HtmlTextWriterStyle.Width, "15px");
        lbLogout.Text = "<a href=\"" + ConfigurationManager.AppSettings["BaseUrl"] + "/Logout.aspx\">" + ResourceHelper.Get("LBL_Logout") + "</a>";
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckSession())
        {
            _sessionActive = false;
            if (_mode == SessionMode.Private)
                Response.Redirect(ConfigurationManager.AppSettings["BaseUrl"] + "/DoLogin.aspx?msg=Login Required.&url=" + HttpUtility.UrlEncode(Request.Url.PathAndQuery));
            else
                this.Visible = false;
        }
        else
        {
            if (_mode == SessionMode.PublicOnly)
            {
                Session.Clear();
                this.Visible = false;
            }
            else
            {
                _sessionActive = true;
                if (Session["FNAME"] != null && Session["LNAME"] != null)
                    lblName.Text = Session["FNAME"].ToString() + " " + Session["LNAME"].ToString();
            }
        }
    }
    private bool CheckSession()
    {
        if (Session["USERID"] != null && Session["USERID"].ToString() != "")
            return true;
        else
            return false;
    }
}
